package com.moody.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moody.hibernate.domain.DebtInfo;
import com.moody.hibernate.domain.RiskReportIRDetail;
import com.moody.hibernate.domain.RiskReportREDetail;
import com.moody.hibernate.domain.UserInfo;
import com.moody.service.CustomerService;
import com.moody.service.ObligationService;
import com.moody.service.RiskReportService;
import com.moody.hibernate.dao.CustInfoDAO;
import com.moody.hibernate.domain.CustInfo;
import com.moody.init.DataDictionary;
import com.moody.service.SystemService;

/*
 * Copyright ? Moody analytics   
 *
 * @Title: RiskReportController.java 
 * @Package： com.moody.web 
 * @author： Liu Peng
 * @Description: 风险报告处理类    
 * @date： 2012-2-29 上午11:21:59
 * @Modified by: Liu Peng 
 * @version V1.0   
 */
@Controller
@RequestMapping("/RiskReport")
public class RiskReportController {
	private static final Logger log = LoggerFactory
			.getLogger(RiskReportController.class);
	@Resource
	private CustomerService customerService;
	@Resource
	RiskReportService riskReportService;
	@Resource
	private SystemService systemService;
	@Resource
	private ObligationService obligationService;

	@RequestMapping("exportExcel")
	public void exportExcel(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		response.setContentType("application/vnd.ms-excel;charset=utf-8");  
		 
		OutputStream os = response.getOutputStream();
		WritableWorkbook wwb = Workbook.createWorkbook(os);
		
		String id = request.getParameter("id");
		String fixedReportType = (String) request.getSession().getAttribute("fixedReportType");
		
		if(fixedReportType == null || fixedReportType.equals("00")){ 
			String statisticsQuery = (String) request.getSession().getAttribute("statisticsQuery");
			if(statisticsQuery == null || statisticsQuery.equals("")) return ;
			else if(id.equals("01") && statisticsQuery.equals("01")){   //id = 01 一般查询
				String custType = (String) request.getSession().getAttribute("custType"); 
				String custName = (String) request.getSession().getAttribute("custName");
				String custNo = (String) request.getSession().getAttribute("custNo");
				String custScope = (String) request.getSession().getAttribute("custScope");
				String phaseStat = (String) request.getSession().getAttribute("phaseStat");
				String orgCode = (String) request.getSession().getAttribute("orgCode");
				String industryType = (String) request.getSession().getAttribute("industryType");
				String creditLevel = (String) request.getSession().getAttribute("creditLevel");
				String defaultStat = (String) request.getSession().getAttribute("defaultStat");
				String exposureType = (String) request.getSession().getAttribute("exposureType");
				String classifyDateStart = (String) request.getSession().getAttribute("classifyDateStart");
				String classifyDateEnd = (String) request.getSession().getAttribute("classifyDateEnd");
				String modelId = (String) request.getSession().getAttribute("modelId");
				
				List<CustInfo> list = new ArrayList<CustInfo>();
				//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
				String exposureTypeString = "";
				if(!exposureType.equals("")){
					exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
				}
				
				Map<String,String> condition = new LinkedHashMap<String,String>();
				if(custType != null && !custType.equals(""))condition.put("custType", custType);
				if(custName != null && !custName.equals(""))condition.put("custName", custName);
				if(custNo != null && !custNo.equals(""))condition.put("custNo", custNo);
				if(custScope != null && !custScope.equals(""))condition.put("custScope", custScope);
				if(orgCode != null && !orgCode.equals(""))condition.put("inputOrgId", orgCode);
				if(industryType != null && !industryType.equals(""))condition.put("industryType", industryType);
				if(defaultStat != null && !defaultStat.equals(""))condition.put("defaultStat", defaultStat);
				if(exposureTypeString != null && !exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
					
				Map<String,String> pdhCondition = new LinkedHashMap<String,String>();
				if(creditLevel != null && !creditLevel.equals(""))pdhCondition.put("confirmPdlevel", creditLevel);
				if(phaseStat != null && !phaseStat.equals(""))pdhCondition.put("phaseStat", phaseStat);
				if(modelId != null && !modelId.equals(""))pdhCondition.put("modelId", modelId);
				
				UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
				String userId = user.getUserId();
				String orgId = user.getBelongorg();
				list = customerService.findCustByPdhCondition(condition,pdhCondition,userId,orgId);

				//excel文件名显示：
//				if(custType != null && !custType.equals("")) custType = "_" + DataDictionary.getInstance().getNameByCodeAndId("CustType", custType);
//				if(industryType != null && !industryType.equals("")) industryType = "_" + DataDictionary.getInstance().getNameByCodeAndId("IndustryType", industryType);
//				if(organizationId != null && !organizationId.equals("")) organizationId = "_" + organizationId;
//				if(creditLevel != null && !creditLevel.equals("")) creditLevel = "_" + DataDictionary.getInstance().getNameByCodeAndId("PDValue", creditLevel);
//				if(defaultStat != null  && !defaultStat.equals("")) defaultStat = "_" + DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", defaultStat);
//				String fileName ="客户列表" + custType+ industryType+organizationId+creditLevel+defaultStat;
				String fileName = "Export";
				
				//fileName = new String(fileName.getBytes(),"utf-8");
				log.info("filename: "+fileName);
				response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");

				Iterator<CustInfo> it = list.iterator();
				while(it.hasNext()){
					CustInfo cust = it.next();
					String confirmDate = cust.getConfirmDate() ;
					if( confirmDate == null || confirmDate.equals("")) {
						it.remove();
						continue;
					}
					if(confirmDate.lastIndexOf('-') - confirmDate.indexOf('-') == 2){//个位数月份,前面加0
						confirmDate = confirmDate.substring(0,confirmDate.indexOf('-') + 1) + "0" + confirmDate.substring(confirmDate.indexOf('-') + 1);
					}
					if(classifyDateEnd.length() - classifyDateEnd.indexOf('-') == 2){//个位数月份,前面加0
						classifyDateEnd = classifyDateEnd.substring(0,classifyDateEnd.indexOf('-') + 1) + "0" + classifyDateEnd.substring(classifyDateEnd.indexOf('-') + 1);
					}
					if(classifyDateStart.length() - classifyDateStart.indexOf('-') == 2){//个位数月份,前面加0
						classifyDateStart = classifyDateStart.substring(0,classifyDateStart.indexOf('-') + 1) + "0" + classifyDateStart.substring(classifyDateStart.indexOf('-') + 1);
					}
					confirmDate = confirmDate.substring(0,confirmDate.lastIndexOf('-'));
					if((!classifyDateStart.equals("") && classifyDateStart.compareTo(confirmDate) > 0) || (!classifyDateEnd.equals("")  && classifyDateEnd.compareTo(confirmDate) < 0)){
						it.remove();
						continue;
					}
					
					if(cust.getCustType() != null && !"".equals(cust.getCustType()))cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
					if(cust.getCustScope() != null && !"".equals(cust.getCustScope()))cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", cust.getCustScope()));
					if(cust.getIndustryType() !=null && !"".equals(cust.getIndustryType()))cust.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", cust.getIndustryType()));
					if(cust.getDefaultStat() !=null && !"".equals(cust.getDefaultStat()))cust.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
//					cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
					if (cust.getInputOrgId() != null && !cust.getInputOrgId().equals("")&& systemService.findOrgById(cust.getInputOrgId()) != null)
						cust.setInputOrgId(systemService.findOrgById(cust.getInputOrgId()).getOrgname());
				}
				if(!classifyDateStart.equals("") && !classifyDateEnd.equals("")  && classifyDateStart.compareTo(classifyDateEnd)> 0)//如果区间无效，则清空列表
					list =  new ArrayList<CustInfo>();

				
				try {

					//新建一张表
					WritableSheet wsheet = wwb.createSheet("查询结果列表",0);
					wsheet.setColumnView(0, 10); // 设置列的宽度
					wsheet.setColumnView(1, 20); // 设置列的宽度
					wsheet.setColumnView(2, 20); // 设置列的宽度
					wsheet.setColumnView(3, 20); // 设置列的宽度
					wsheet.setColumnView(4, 20); // 设置列的宽度
					wsheet.setColumnView(5, 20); // 设置列的宽度
					wsheet.setColumnView(6, 20); // 设置列的宽度
					wsheet.setColumnView(7, 20); // 设置列的宽度
					wsheet.setColumnView(8, 20); // 设置列的宽度
					wsheet.setColumnView(9, 20); // 设置列的宽度
					wsheet.setColumnView(10, 20); // 设置列的宽度
					wsheet.setColumnView(11, 20); // 设置列的宽度
					wsheet.setColumnView(12, 20); // 设置列的宽度
					
					//设置表头
					WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
							WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
					wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
					wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
					
					WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
							WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
					wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
							

					Label label = new Label(0,0,"编号",wcf);
					wsheet.addCell(label);
					label = new Label(1,0,"客户号",wcf);
					wsheet.addCell(label);
					label = new Label(2,0,"客户名称",wcf);
					wsheet.addCell(label);
					label = new Label(3,0,"客户类型",wcf);
					wsheet.addCell(label);
					label = new Label(4,0,"所属行业",wcf);
					wsheet.addCell(label);
					label = new Label(5,0,"客户规模",wcf);
					wsheet.addCell(label);
					label = new Label(6,0,"信用等级",wcf);
					wsheet.addCell(label);
					label = new Label(7,0,"所属机构",wcf);
					wsheet.addCell(label);
					label = new Label(8,0,"信贷余额",wcf);
					wsheet.addCell(label);
					label = new Label(9,0,"风险暴露分类",wcf);
					wsheet.addCell(label);
					label = new Label(10,0,"违约状态",wcf);
					wsheet.addCell(label);
					label = new Label(11,0,"经办人",wcf);
					wsheet.addCell(label);
					label = new Label(12,0,"信用认定日期",wcf);
					wsheet.addCell(label);
					
					it = list.iterator();
					int lineNum = 1;
					while(it.hasNext()){
						CustInfo item = it.next();
						
						label = new Label(0,lineNum,""+lineNum,wcf1);
						wsheet.addCell(label);
						label = new Label(1,lineNum,item.getCustNo(),wcf1);
						wsheet.addCell(label);
						label = new Label(2,lineNum,item.getCustName(),wcf1);
						wsheet.addCell(label);
						label = new Label(3,lineNum,item.getCustType(),wcf1);
						wsheet.addCell(label);
						label = new Label(4,lineNum,item.getIndustryType(),wcf1);
						wsheet.addCell(label);
						label = new Label(5,lineNum,item.getCustScope(),wcf1);
						wsheet.addCell(label);
						label = new Label(6,lineNum,item.getCreditLevel(),wcf1);
						wsheet.addCell(label);
						label = new Label(7,lineNum,item.getInputOrgId(),wcf1);
						wsheet.addCell(label);
						label = new Label(8,lineNum,""+item.getBalance(),wcf1);
						wsheet.addCell(label);
						label = new Label(9,lineNum,item.getExposureType(),wcf1);
						wsheet.addCell(label);
						label = new Label(10,lineNum,item.getDefaultStat(),wcf1);
						wsheet.addCell(label);
						label = new Label(11,lineNum,item.getInputUserName(),wcf1);
						wsheet.addCell(label);
						label = new Label(12,lineNum,item.getConfirmDate(),wcf1);
						wsheet.addCell(label);
						
						lineNum++;
						
					}
					
					wwb.write();
					wwb.close();
					
				} catch (RowsExceededException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
				
				  
				  os.close();
				  response.flushBuffer();
				
			}
			else if(id.equals("02") && statisticsQuery.equals("02")){//id=02 综合查询-客户查询
				String custType = (String) request.getSession().getAttribute("custType"); 
				String custScope = (String) request.getSession().getAttribute("custScope");
				String exposureType = (String) request.getSession().getAttribute("exposureType");
				String orgCode = (String) request.getSession().getAttribute("orgCode");
				String industryType = (String) request.getSession().getAttribute("industryType");
				String creditLevel = (String) request.getSession().getAttribute("creditLevel");
				String defaultStat = (String) request.getSession().getAttribute("defaultStat");
				
				List<CustInfo> list = new ArrayList<CustInfo>();

				//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
				String exposureTypeString = "";
				if(!exposureType.equals("")){
					exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
				}
				
				Map<String,String> condition = new LinkedHashMap<String,String>();
				if(custType != null && !custType.equals(""))condition.put("custType", custType);
				if(custScope != null && !custScope.equals(""))condition.put("custScope", custScope);
				if(orgCode != null && !orgCode.equals(""))condition.put("inputOrgId", orgCode);
				if(industryType != null && !industryType.equals(""))condition.put("industryType", industryType);
				if(creditLevel != null && !creditLevel.equals(""))condition.put("creditLevel", creditLevel);
				if(defaultStat != null && !defaultStat.equals(""))condition.put("defaultStat", defaultStat);
				if(exposureTypeString != null && !exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
				
				UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
				String userId = user.getUserId();
				String orgId = user.getBelongorg();
				list = customerService.findCustByCondition(condition,userId,orgId);
				
//				list = customerService.findBiggestCustByCondition(custType,custScope, exposureTypeString, orgCode,industryType, creditLevel,defaultStat,userId,orgId);

				//excel文件名显示：
				if(custType != null && !custType.equals("")) custType = "_" + DataDictionary.getInstance().getNameByCodeAndId("CustType", custType);
				if(industryType != null && !industryType.equals("")) industryType = "_" + DataDictionary.getInstance().getNameByCodeAndId("IndustryType", industryType);
				if(creditLevel != null && !creditLevel.equals("")) creditLevel = "_" + DataDictionary.getInstance().getNameByCodeAndId("PDValue", creditLevel);
				if(defaultStat != null  && !defaultStat.equals("")) defaultStat = "_" + DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", defaultStat);
//				String fileName ="客户列表" + custType+ industryType+creditLevel+defaultStat;
//				fileName = new String(fileName.getBytes(),"utf-8");
				String fileName = "Export";
				response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");

				
				for (CustInfo cust : list) {
					cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
					cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", cust.getCustScope()));
					cust.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", cust.getIndustryType()));
					cust.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
					cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
					if (cust.getInputOrgId() != null && !cust.getInputOrgId().equals("")&& systemService.findOrgById(cust.getInputOrgId()) != null)
						cust.setInputOrgId(systemService.findOrgById(cust.getInputOrgId()).getOrgname());
				}
				
				try {

					//新建一张表
					WritableSheet wsheet = wwb.createSheet("查询结果列表",0);
					wsheet.setColumnView(0, 10); // 设置列的宽度
					wsheet.setColumnView(1, 20); // 设置列的宽度
					wsheet.setColumnView(2, 20); // 设置列的宽度
					wsheet.setColumnView(3, 20); // 设置列的宽度
					wsheet.setColumnView(4, 20); // 设置列的宽度
					wsheet.setColumnView(5, 20); // 设置列的宽度
					wsheet.setColumnView(6, 20); // 设置列的宽度
					wsheet.setColumnView(7, 20); // 设置列的宽度
					wsheet.setColumnView(8, 20); // 设置列的宽度
					wsheet.setColumnView(9, 20); // 设置列的宽度
					wsheet.setColumnView(10, 20); // 设置列的宽度
					wsheet.setColumnView(11, 20); // 设置列的宽度
					wsheet.setColumnView(12, 20); // 设置列的宽度
					
					//设置表头
					WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
							WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
					wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
					wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
					
					WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
							WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
					wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
							

					Label label = new Label(0,0,"编号",wcf);
					wsheet.addCell(label);
					label = new Label(1,0,"客户号",wcf);
					wsheet.addCell(label);
					label = new Label(2,0,"客户名称",wcf);
					wsheet.addCell(label);
					label = new Label(3,0,"客户类型",wcf);
					wsheet.addCell(label);
					label = new Label(4,0,"所属行业",wcf);
					wsheet.addCell(label);
					label = new Label(5,0,"客户规模",wcf);
					wsheet.addCell(label);
					label = new Label(6,0,"信用等级",wcf);
					wsheet.addCell(label);
					label = new Label(7,0,"评级日期",wcf);
					wsheet.addCell(label);
					label = new Label(8,0,"信贷余额",wcf);
					wsheet.addCell(label);
					label = new Label(9,0,"风险暴露分类",wcf);
					wsheet.addCell(label);
					label = new Label(10,0,"违约状态",wcf);
					wsheet.addCell(label);
					label = new Label(11,0,"所属机构",wcf);
					wsheet.addCell(label);
					label = new Label(12,0,"经办人",wcf);
					wsheet.addCell(label);
					
					Iterator<CustInfo> it = list.iterator();
					int lineNum = 1;
					while(it.hasNext()){
						CustInfo item = it.next();
						
						label = new Label(0,lineNum,""+lineNum,wcf1);
						wsheet.addCell(label);
						label = new Label(1,lineNum,item.getCustNo(),wcf1);
						wsheet.addCell(label);
						label = new Label(2,lineNum,item.getCustName(),wcf1);
						wsheet.addCell(label);
						label = new Label(3,lineNum,item.getCustType(),wcf1);
						wsheet.addCell(label);
						label = new Label(4,lineNum,item.getIndustryType(),wcf1);
						wsheet.addCell(label);
						label = new Label(5,lineNum,item.getCustScope(),wcf1);
						wsheet.addCell(label);
						label = new Label(6,lineNum,item.getCreditLevel(),wcf1);
						wsheet.addCell(label);
						label = new Label(7,lineNum,item.getConfirmDate(),wcf1);
						wsheet.addCell(label);
						label = new Label(8,lineNum,""+item.getBalance(),wcf1);
						wsheet.addCell(label);
						label = new Label(9,lineNum,item.getExposureType(),wcf1);
						wsheet.addCell(label);
						label = new Label(10,lineNum,item.getDefaultStat(),wcf1);
						wsheet.addCell(label);
						label = new Label(11,lineNum,item.getInputOrgName(),wcf1);
						wsheet.addCell(label);
						label = new Label(12,lineNum,item.getInputUserName(),wcf1);
						wsheet.addCell(label);
						
						lineNum++;
						
					}
					
					wwb.write();
					wwb.close();
					
				} catch (RowsExceededException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
				
				  
				  os.close();
				  response.flushBuffer();
			}
			else if(id.equals("03") && statisticsQuery.equals("03")){ //id = 03 综合查询-债项查询
				String custName = (String) request.getSession().getAttribute("custName");
				String custType = (String) request.getSession().getAttribute("custType");
				String custScope = (String) request.getSession().getAttribute("custScope");
				String industryType = (String) request.getSession().getAttribute("industryType");
				String exposureType = (String) request.getSession().getAttribute("exposureType");
				String orgCode = (String) request.getSession().getAttribute("orgCode");
				String debtNo = (String) request.getSession().getAttribute("debtNo");
				String debtName = (String) request.getSession().getAttribute("debtName");
				String debtType = (String) request.getSession().getAttribute("debtType");
				String guarantyKind = (String) request.getSession().getAttribute("guarantyKind");
				
				String classifyResult = (String) request.getSession().getAttribute("classifyResult");
				String classifyDate = (String) request.getSession().getAttribute("classifyDate");
				
				
				//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
				String exposureTypeString = "";
				if(!exposureType.equals("")){
					exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
				}
				Map<String,String> custCondition = new LinkedHashMap<String,String>();
				Map<String,String> debtCondition = new LinkedHashMap<String,String>();
				//key需要和domain对象的属性相同
				if(!custName.equals(""))custCondition.put("custName", custName);
				if(!custType.equals(""))custCondition.put("custType", custType);
				if(!custScope.equals(""))custCondition.put("custScope", custScope);
				if(!industryType.equals(""))custCondition.put("industryType", industryType);
				if(!exposureTypeString.equals(""))custCondition.put("exposureType", exposureTypeString);
				if(!orgCode.equals(""))custCondition.put("inputOrgId", orgCode);
				
				if(!debtNo.equals(""))debtCondition.put("debtNo", debtNo);
				if(!debtName.equals(""))debtCondition.put("debtDesc", debtName);
				if(!debtType.equals(""))debtCondition.put("debtType", debtType);
				if(!guarantyKind.equals(""))debtCondition.put("guarantyKind", guarantyKind);
				if(!classifyResult.equals(""))debtCondition.put("classifyResult", classifyResult);
				if(!classifyDate.equals(""))debtCondition.put("classifyDate", classifyDate);
				
				UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
				String userId = user.getUserId();
				String orgId = user.getBelongorg();
				List<DebtInfo> list = new ArrayList<DebtInfo>();
				list = customerService.findDebtByCondition(custCondition, debtCondition, userId, orgId);
				
//				list = customerService.findDebtByCondition(custName,custType, custScope, industryType, exposureTypeString, orgCode,debtNo, debtName,debtType, guarantyKind,classifyResult,classifyDate);

				//excel文件名显示：
				if(custName != null && !custName.equals("")) custName = "_" + custName;
				if(debtNo != null && !debtNo.equals("")) debtNo = "_" + debtNo;
				if(debtName != null && !debtName.equals("")) debtName = "_" + debtName;
				if(debtType != null  && !debtType.equals("")) debtType = "_" + DataDictionary.getInstance().getNameByCodeAndId("DebtType", debtType);
//				String fileName ="债项列表" + custName+ debtNo+debtName+debtType;
//				fileName = new String(fileName.getBytes(),"utf-8");
				String fileName = "Export";
				response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
				
				
				for (DebtInfo debt : list) {
					debt.setDebtType(DataDictionary.getInstance().getNameByCodeAndId("DebtType", debt.getDebtType()));
					debt.setGuarantyKind(DataDictionary.getInstance().getNameByCodeAndId("GuarantyKind", debt.getGuarantyKind()));
					debt.setClassifyResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", debt.getClassifyResult()));
				}
				
				try {

					//新建一张表
					WritableSheet wsheet = wwb.createSheet("查询结果列表",0);
					wsheet.setColumnView(0, 10); // 设置列的宽度
					wsheet.setColumnView(1, 20); // 设置列的宽度
					wsheet.setColumnView(2, 20); // 设置列的宽度
					wsheet.setColumnView(3, 20); // 设置列的宽度
					wsheet.setColumnView(4, 20); // 设置列的宽度
					wsheet.setColumnView(5, 20); // 设置列的宽度
					wsheet.setColumnView(6, 20); // 设置列的宽度
					wsheet.setColumnView(7, 20); // 设置列的宽度
					wsheet.setColumnView(8, 20); // 设置列的宽度
					wsheet.setColumnView(9, 20); // 设置列的宽度
					wsheet.setColumnView(10, 20); // 设置列的宽度
					
					//设置表头
					WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
							WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
					wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
					wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
					
					WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
							WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
					WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
					wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
							

					Label label = new Label(0,0,"编号",wcf);
					wsheet.addCell(label);
					label = new Label(1,0,"债项编号",wcf);
					wsheet.addCell(label);
					label = new Label(2,0,"客户名称",wcf);
					wsheet.addCell(label);
					label = new Label(3,0,"债项名称",wcf);
					wsheet.addCell(label);
					label = new Label(4,0,"债项业务品种",wcf);
					wsheet.addCell(label);
					label = new Label(5,0,"合同金额",wcf);
					wsheet.addCell(label);
					label = new Label(6,0,"债项余额",wcf);
					wsheet.addCell(label);
					label = new Label(7,0,"资产分类",wcf);
					wsheet.addCell(label);
					label = new Label(8,0,"担保类型",wcf);
					wsheet.addCell(label);
					label = new Label(9,0,"认定日期",wcf);
					wsheet.addCell(label);
					label = new Label(10,0,"所属机构",wcf);
					wsheet.addCell(label);
					
					Iterator<DebtInfo> it = list.iterator();
					int lineNum = 1;
					while(it.hasNext()){
						DebtInfo item = it.next();
						
						label = new Label(0,lineNum,""+lineNum,wcf1);
						wsheet.addCell(label);
						label = new Label(1,lineNum,item.getDebtNo(),wcf1);
						wsheet.addCell(label);
						label = new Label(2,lineNum,item.getCustName(),wcf1);
						wsheet.addCell(label);
						label = new Label(3,lineNum,item.getDebtDesc(),wcf1);
						wsheet.addCell(label);
						label = new Label(4,lineNum,item.getDebtType(),wcf1);
						wsheet.addCell(label);
						label = new Label(5,lineNum,"" + item.getDebtSum(),wcf1);
						wsheet.addCell(label);
						label = new Label(6,lineNum,"" + item.getDebtBalance(),wcf1);
						wsheet.addCell(label);
						label = new Label(7,lineNum,item.getClassifyResult(),wcf1);
						wsheet.addCell(label);
						label = new Label(8,lineNum,item.getGuarantyKind(),wcf1);
						wsheet.addCell(label);
						label = new Label(9,lineNum,item.getClassifyDate(),wcf1);
						wsheet.addCell(label);
						label = new Label(10,lineNum,item.getInputOrgId(),wcf1);
						wsheet.addCell(label);
						
						lineNum++;
						
					}
					
					wwb.write();
					wwb.close();
					
				} catch (RowsExceededException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
				
				
				
				
				
			}
		}
		else if(id.equals("11") && fixedReportType.equals("01")){//id = 11 非零售内部评级明细表
			
			String queryCustType2 = (String)request.getSession().getAttribute("queryCustType2");
			String date = (String)request.getSession().getAttribute("date");
			String orgId = (String) request.getSession().getAttribute("orgId");
			String userId = (String) request.getSession().getAttribute("userId");
			
//			String queryCustType2String = (String)request.getSession().getAttribute("queryCustType2String");
//			String fileName ="非零售内部评级明细表_" + queryCustType2String+ "_" + orgId+"_"+ userId+"_"+date;
//			fileName = new String(fileName.getBytes(),"utf-8");
			String fileName = "Export";
			response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
			
			UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
			String logonUserId = user.getUserId();
			String logonOrgId = user.getBelongorg();
			
			List<RiskReportIRDetail> list = riskReportService.getIRDetailByCondition(queryCustType2, date, orgId, userId,logonUserId,logonOrgId);
			
			
			try {

				//新建一张表
				WritableSheet wsheet = wwb.createSheet("非零售内部评级明细表",0);
				wsheet.setColumnView(0, 10); // 设置列的宽度
				wsheet.setColumnView(1, 20); // 设置列的宽度
				wsheet.setColumnView(2, 20); // 设置列的宽度
				wsheet.setColumnView(3, 20); // 设置列的宽度
				wsheet.setColumnView(4, 20); // 设置列的宽度
				wsheet.setColumnView(5, 20); // 设置列的宽度
				wsheet.setColumnView(6, 20); // 设置列的宽度
				wsheet.setColumnView(7, 20); // 设置列的宽度
				wsheet.setColumnView(8, 20); // 设置列的宽度
				wsheet.setColumnView(9, 20); // 设置列的宽度
				wsheet.setColumnView(10, 20); // 设置列的宽度
				wsheet.setColumnView(11, 20); // 设置列的宽度
				wsheet.setColumnView(12, 20); // 设置列的宽度
				wsheet.setColumnView(13, 20); // 设置列的宽度
				wsheet.setColumnView(14, 20); // 设置列的宽度
				
				//设置表头
				WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
						WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
						jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
				WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
				wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
				wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
				
				WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
						WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
						jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
				WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
				wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
						

				Label label = new Label(0,0,"编号",wcf);
				wsheet.addCell(label);
				label = new Label(1,0,"客户号",wcf);
				wsheet.addCell(label);
				label = new Label(2,0,"客户名称",wcf);
				wsheet.addCell(label);
				label = new Label(3,0,"客户类型",wcf);
				wsheet.addCell(label);
				label = new Label(4,0,"所属行业",wcf);
				wsheet.addCell(label);
				label = new Label(5,0,"组织形式",wcf);
				wsheet.addCell(label);
				label = new Label(6,0,"企业规模",wcf);
				wsheet.addCell(label);
				label = new Label(7,0,"建立信贷关系时间",wcf);
				wsheet.addCell(label);
				label = new Label(8,0,"所属区域",wcf);
				wsheet.addCell(label);
				label = new Label(9,0,"风险暴露分类",wcf);
				wsheet.addCell(label);
				label = new Label(10,0,"最新评级财报期次",wcf);
				wsheet.addCell(label);
				label = new Label(11,0,"最新评级",wcf);
				wsheet.addCell(label);
				label = new Label(12,0,"评级发起机构",wcf);
				wsheet.addCell(label);
				label = new Label(13,0,"评级认定机构",wcf);
				wsheet.addCell(label);
				label = new Label(14,0,"评级认定时间",wcf);
				wsheet.addCell(label);
				
				Iterator<RiskReportIRDetail> it = list.iterator();
				int lineNum = 1;
				while(it.hasNext()){
					RiskReportIRDetail item = it.next();
					CustInfo cust = customerService.findCustByCustId(item.getCustID());
					if(cust == null ){
						it.remove();
						continue;
					}
					label = new Label(0,lineNum,""+lineNum,wcf1);
					wsheet.addCell(label);
					label = new Label(1,lineNum,cust.getCustNo(),wcf1);
					wsheet.addCell(label);
					label = new Label(2,lineNum,item.getCustName(),wcf1);
					wsheet.addCell(label);
					label = new Label(3,lineNum,item.getCustType(),wcf1);
					wsheet.addCell(label);
					label = new Label(4,lineNum,item.getIndustryType(),wcf1);
					wsheet.addCell(label);
					label = new Label(5,lineNum,item.getOrgType(),wcf1);
					wsheet.addCell(label);
					label = new Label(6,lineNum,item.getCustScope(),wcf1);
					wsheet.addCell(label);
					label = new Label(7,lineNum,item.getCreditTime(),wcf1);
					wsheet.addCell(label);
					label = new Label(8,lineNum,item.getArea(),wcf1);
					wsheet.addCell(label);
					label = new Label(9,lineNum,item.getExposureType(),wcf1);
					wsheet.addCell(label);
					label = new Label(10,lineNum,item.getNewestPDReportNo(),wcf1);
					wsheet.addCell(label);
					label = new Label(11,lineNum,item.getNewestPD(),wcf1);
					wsheet.addCell(label);
					label = new Label(12,lineNum,item.getFirstOrgID(),wcf1);
					wsheet.addCell(label);
					label = new Label(13,lineNum,item.getConfirmOrgID(),wcf1);
					wsheet.addCell(label);
					label = new Label(14,lineNum,item.getConfirmDate(),wcf1);
					wsheet.addCell(label);
					
					lineNum++;
					
				}
				
				wwb.write();
				wwb.close();
				
			} catch (RowsExceededException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			}
			
			  
			  os.close();
			  response.flushBuffer();
			
		}

			
		else if(id.equals("12") && fixedReportType.equals("02")){//id = 12 非零售风险暴露明细表
			String queryCustType2 = (String)request.getSession().getAttribute("queryCustType2");
			String date = (String)request.getSession().getAttribute("date");
			String orgId = (String) request.getSession().getAttribute("orgId");
			String userId = (String) request.getSession().getAttribute("userId");
			String currency = (String) request.getSession().getAttribute("currency");
			
//			String queryCustType2String = (String)request.getSession().getAttribute("queryCustType2String");	
//			String fileName ="非零售风险暴露明细表_" + queryCustType2String+ "_" + orgId+"_"+ userId+"_"+date;
//			fileName = new String(fileName.getBytes(),"utf-8");
			String fileName = "Export";
			response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
			
			UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
			String logonUserId = user.getUserId();
			String logonOrgId = user.getBelongorg();
			List<RiskReportREDetail> list = riskReportService.getREDetailByCondition(queryCustType2, date, orgId, userId,currency,logonUserId,logonOrgId);
			
			
			try {

				//新建一张表
				WritableSheet wsheet = wwb.createSheet("非零售风险暴露明细表",0);
				wsheet.setColumnView(0, 10); // 设置列的宽度
				wsheet.setColumnView(1, 20); // 设置列的宽度
				wsheet.setColumnView(2, 20); // 设置列的宽度
				wsheet.setColumnView(3, 20); // 设置列的宽度
				wsheet.setColumnView(4, 20); // 设置列的宽度
				wsheet.setColumnView(5, 20); // 设置列的宽度
				wsheet.setColumnView(6, 20); // 设置列的宽度
				wsheet.setColumnView(7, 20); // 设置列的宽度
				wsheet.setColumnView(8, 20); // 设置列的宽度
				wsheet.setColumnView(9, 20); // 设置列的宽度
				wsheet.setColumnView(10, 20); // 设置列的宽度
				
				//设置表头
				WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
						WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
						jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
				WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
				wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
				wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
				
				WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
						WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
						jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
				WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
				wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
						

				Label label = new Label(0,0,"编号",wcf);
				wsheet.addCell(label);
				label = new Label(1,0,"客户号",wcf);
				wsheet.addCell(label);
				label = new Label(2,0,"客户名称",wcf);
				wsheet.addCell(label);
				label = new Label(3,0,"客户评级",wcf);
				wsheet.addCell(label);
				label = new Label(4,0,"所属行业",wcf);
				wsheet.addCell(label);
				label = new Label(5,0,"所属区域",wcf);
				wsheet.addCell(label);
				label = new Label(6,0,"债项编号",wcf);
				wsheet.addCell(label);
				label = new Label(7,0,"业务品种",wcf);
				wsheet.addCell(label);
				label = new Label(8,0,"风险暴露分类",wcf);
				wsheet.addCell(label);
				label = new Label(9,0,"风险暴露余额",wcf);
				wsheet.addCell(label);
				label = new Label(10,0,"经办人",wcf);
				
				Iterator<RiskReportREDetail> it = list.iterator();
				int lineNum = 1;
				while(it.hasNext()){
					RiskReportREDetail item = it.next();

					CustInfo cust = customerService.findCustByCustId(item.getCustID());
					if(cust == null ){
						it.remove();
						continue;
					}
					label = new Label(0,lineNum,""+lineNum,wcf1);
					wsheet.addCell(label);
					label = new Label(1,lineNum,cust.getCustNo(),wcf1);
					wsheet.addCell(label);
					label = new Label(2,lineNum,item.getCustName(),wcf1);
					wsheet.addCell(label);
					label = new Label(3,lineNum,item.getSystemPDLevel(),wcf1);
					wsheet.addCell(label);
					label = new Label(4,lineNum,item.getIndustryType(),wcf1);
					wsheet.addCell(label);
					label = new Label(5,lineNum,item.getArea(),wcf1);
					wsheet.addCell(label);
					label = new Label(6,lineNum,item.getDebtNo(),wcf1);
					wsheet.addCell(label);
					label = new Label(7,lineNum,item.getDebtType(),wcf1);
					wsheet.addCell(label);
					label = new Label(8,lineNum,item.getExposureType(),wcf1);
					wsheet.addCell(label);
					label = new Label(9,lineNum,item.getDebtBalance(),wcf1);
					wsheet.addCell(label);
					label = new Label(10,lineNum,item.getOperator(),wcf1);
					
					lineNum++;
					
				}
				
				wwb.write();
				wwb.close();
				
			} catch (RowsExceededException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			}
			
			  
			  os.close();
			  response.flushBuffer();
			
		}
		
	}
	
	
	@RequestMapping("getFixedReport")
	public String getFixedReport(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		
		List list = new ArrayList();
		
		String fixedReportType = request.getParameter("fixedReportType");
		String queryCustType2 = request.getParameter("queryCustType2");
		String orgId = request.getParameter("orgId");
		String userId = request.getParameter("userId");
		String date = request.getParameter("date");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String currency = request.getParameter("currency");
		String currencyUnit = request.getParameter("currencyUnit");
		
		if(queryCustType2 == null || queryCustType2.equals("")) queryCustType2 = "";
		if(orgId == null || orgId.equals("")) orgId = "";//默认是用户所在机构
		if(userId == null || userId.equals("")) userId = "";
		if(currency == null || currency.equals("")) currency = "01";
		if(currencyUnit == null || currencyUnit.equals("")) currencyUnit = "03";
		if("".equals(date) || date == null){//默认为上一个月
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M");
			Calendar lastDate = Calendar.getInstance();
			lastDate.add(Calendar.MONTH,-1);
			date = sdf.format(lastDate.getTime());
		}
		else{//去除月份数字前的“0”
			StringTokenizer dateTonkens = new StringTokenizer(date, "-");
			String year = dateTonkens.nextToken();
			String month = dateTonkens.nextToken();
			month = "" + Integer.parseInt(month);
			date = year + "-" + month;
		}

		request.getSession().removeAttribute("fixedReportType");
		request.getSession().setAttribute("fixedReportType", fixedReportType);
		request.getSession().removeAttribute("queryCustType2");
		request.getSession().setAttribute("queryCustType2", queryCustType2);
		request.getSession().removeAttribute("orgId");
		request.getSession().setAttribute("orgId", orgId);
		request.getSession().removeAttribute("userId");
		request.getSession().setAttribute("userId", userId);
		request.getSession().removeAttribute("date");
		request.getSession().setAttribute("date", date);
		request.getSession().removeAttribute("startDate");
		request.getSession().setAttribute("startDate", startDate);
		request.getSession().removeAttribute("endDate");
		request.getSession().setAttribute("endDate", endDate);
		request.getSession().removeAttribute("currency");
		request.getSession().setAttribute("currency", currency);
		request.getSession().removeAttribute("currencyUnit");
		request.getSession().setAttribute("currencyUnit", currencyUnit);
		//下几条为页面显示需要
		request.getSession().removeAttribute("queryCustType2String");
		if(queryCustType2.equals(""))
			request.getSession().setAttribute("queryCustType2String", "非零售客户");
		else
			request.getSession().setAttribute("queryCustType2String", DataDictionary.getInstance().getNameByCodeAndId("QueryCustType2", queryCustType2));
		request.getSession().removeAttribute("currencyString");
		request.getSession().setAttribute("currencyString", DataDictionary.getInstance().getNameByCodeAndId("Currency", currency));
		
		if(fixedReportType == null)
			return "risk_report/FixedReportQuery";
		else if(fixedReportType.equals("01")){
			request.getSession().removeAttribute("fixedReportTypeString");
			request.getSession().setAttribute("fixedReportTypeString", "非零售内部评级明细表");
			return "risk_report/FixedReport_IRDetail";
		}

			
		else if(fixedReportType.equals("02")){
			request.getSession().removeAttribute("fixedReportTypeString");
			request.getSession().setAttribute("fixedReportTypeString", "非零售风险暴露明细表");
			return "risk_report/FixedReport_REDetail";
		}
		else
			return "risk_report/FixedReportQuery";
		
		
	}
	
	@RequestMapping("listIRDetail")
	public void listIRDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		
		
		String queryCustType2 = (String)request.getSession().getAttribute("queryCustType2");
		String date = (String)request.getSession().getAttribute("date");
		String orgId = (String) request.getSession().getAttribute("orgId");
		String userId = (String) request.getSession().getAttribute("userId");
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String logonUserId = user.getUserId();
		String logonOrgId = user.getBelongorg();

		List<RiskReportIRDetail> list = riskReportService.getIRDetailByCondition(queryCustType2, date, orgId, userId,logonUserId,logonOrgId);
		
		JSONArray resultJA = JSONArray.fromObject(list);
		out.print(resultJA);
	}
	
	@RequestMapping("listREDetail")
	public void listREDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		
		String queryCustType2 = (String)request.getSession().getAttribute("queryCustType2");
		String date = (String)request.getSession().getAttribute("date");
		String orgId = (String) request.getSession().getAttribute("orgId");
		String userId = (String) request.getSession().getAttribute("userId");
		String currency = (String) request.getSession().getAttribute("currency");
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String logonUserId = user.getUserId();
		String logonOrgId = user.getBelongorg();
		
		List<RiskReportREDetail> list = riskReportService.getREDetailByCondition(queryCustType2, date, orgId, userId,currency,logonUserId,logonOrgId);
		
		JSONArray resultJA = JSONArray.fromObject(list);
		out.print(resultJA);
	}

	// 查找客户
	@RequestMapping("/findCustomer")
	public void findCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custType = request.getParameter("custType");
		String custName = request.getParameter("custName");
		String custNo = request.getParameter("custNo");
		String custScope = request.getParameter("custScope");
		String phaseStat = request.getParameter("phaseStat");
		String orgCode = request.getParameter("orgCode");
		String creditLevel = request.getParameter("pdValue");
		String defaultStat = request.getParameter("defaultStat");
		String exposureType = request.getParameter("exposureType");
		String classifyDateStart = request.getParameter("classifyDateStart");
		String classifyDateEnd = request.getParameter("classifyDateEnd");
		String industryTypeOne = request.getParameter("industryTypeLevelOne");
		String industryTypeTwo = request.getParameter("industryTypeLevelTwo");
		String modelId = request.getParameter("modelId");
		
		String industryType = "";
		if(industryTypeTwo.equals("")){
			if(!industryTypeOne.equals("")) industryType = new StringTokenizer(industryTypeOne).nextToken();
		}
		else industryType = industryTypeTwo;

		if("".equals(classifyDateStart) || classifyDateStart == null){//不处理
			classifyDateStart = "";
		}
		else{//去除月份数字前的“0”
			StringTokenizer dateTonkens = new StringTokenizer(classifyDateStart, "-");
			String year = dateTonkens.nextToken();
			String month = dateTonkens.nextToken();
			month = "" + Integer.parseInt(month);
			classifyDateStart = year + "-" + month;
		}
		
		if("".equals(classifyDateEnd) || classifyDateEnd == null){//不处理
			classifyDateEnd = "";
		}
		else{//去除月份数字前的“0”
			StringTokenizer dateTonkens = new StringTokenizer(classifyDateEnd, "-");
			String year = dateTonkens.nextToken();
			String month = dateTonkens.nextToken();
			month = "" + Integer.parseInt(month);
			classifyDateEnd = year + "-" + month;
		}
		
		request.getSession().setAttribute("custType", custType);
		request.getSession().setAttribute("custName", custName);
		request.getSession().setAttribute("custNo", custNo);
		request.getSession().setAttribute("custScope", custScope);
		request.getSession().setAttribute("phaseStat", phaseStat);
		request.getSession().setAttribute("orgCode", orgCode);
		request.getSession().setAttribute("industryType", industryType);
		request.getSession().setAttribute("creditLevel", creditLevel);
		request.getSession().setAttribute("defaultStat", defaultStat);
		request.getSession().setAttribute("exposureType", exposureType);
		request.getSession().setAttribute("classifyDateStart", classifyDateStart);
		request.getSession().setAttribute("classifyDateEnd", classifyDateEnd);
		request.getSession().setAttribute("modelId", modelId);
		
		request.getSession().setAttribute("fixedReportType", "00");//表明非固定报表
		request.getSession().setAttribute("statisticsQuery", "01");//一般查询

		List<CustInfo> list = new ArrayList<CustInfo>();
		
		//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
		String exposureTypeString = "";
		if(!exposureType.equals("")){
			exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
		}
		
		Map<String,String> condition = new LinkedHashMap<String,String>();

		if(custType != null && !custType.equals(""))condition.put("custType", custType);
		if(custName != null && !custName.equals(""))condition.put("custName", custName);
		if(custNo != null && !custNo.equals(""))condition.put("custNo", custNo);
		if(custScope != null && !custScope.equals(""))condition.put("custScope", custScope);
		if(orgCode != null && !orgCode.equals(""))condition.put("inputOrgId", orgCode);
		if(industryType != null && !industryType.equals(""))condition.put("industryType", industryType);
		if(defaultStat != null && !defaultStat.equals(""))condition.put("defaultStat", defaultStat);
		if(exposureTypeString != null && !exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
			
		Map<String,String> pdhCondition = new LinkedHashMap<String,String>();
		if(creditLevel != null && !creditLevel.equals(""))pdhCondition.put("confirmPdlevel", creditLevel);
		if(phaseStat != null && !phaseStat.equals(""))pdhCondition.put("phaseStat", phaseStat);
		if(modelId != null && !modelId.equals(""))pdhCondition.put("modelId", modelId);
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		list = customerService.findCustByPdhCondition(condition,pdhCondition,userId,orgId);

		Iterator<CustInfo> it = list.iterator();
		while(it.hasNext()){
			CustInfo cust = it.next();
			String confirmDate = cust.getConfirmDate() ;
			if( confirmDate == null || confirmDate.equals("")) {
				it.remove();
				continue;
			}
			if(confirmDate.lastIndexOf('-') - confirmDate.indexOf('-') == 2){//个位数月份,前面加0
				confirmDate = confirmDate.substring(0,confirmDate.indexOf('-') + 1) + "0" + confirmDate.substring(confirmDate.indexOf('-') + 1);
			}
			if(classifyDateEnd.length() - classifyDateEnd.indexOf('-') == 2){//个位数月份,前面加0
				classifyDateEnd = classifyDateEnd.substring(0,classifyDateEnd.indexOf('-') + 1) + "0" + classifyDateEnd.substring(classifyDateEnd.indexOf('-') + 1);
			}
			if(classifyDateStart.length() - classifyDateStart.indexOf('-') == 2){//个位数月份,前面加0
				classifyDateStart = classifyDateStart.substring(0,classifyDateStart.indexOf('-') + 1) + "0" + classifyDateStart.substring(classifyDateStart.indexOf('-') + 1);
			}
			confirmDate = confirmDate.substring(0,confirmDate.lastIndexOf('-'));
			if((!classifyDateStart.equals("") && classifyDateStart.compareTo(confirmDate) > 0) || (!classifyDateEnd.equals("")  && classifyDateEnd.compareTo(confirmDate) < 0)){
				it.remove();
				continue;
			}
			
			if(cust.getCustType() != null && !"".equals(cust.getCustType()))cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
			if(cust.getCustScope() != null && !"".equals(cust.getCustScope()))cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", cust.getCustScope()));
			if(cust.getIndustryType() !=null && !"".equals(cust.getIndustryType()))cust.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", cust.getIndustryType()));
			if(cust.getDefaultStat() !=null && !"".equals(cust.getDefaultStat()))cust.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
//			cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
			if (cust.getInputOrgId() != null && !cust.getInputOrgId().equals("")&& systemService.findOrgById(cust.getInputOrgId()) != null)
				cust.setInputOrgId(systemService.findOrgById(cust.getInputOrgId()).getOrgname());
		}
		if(!classifyDateStart.equals("") && !classifyDateEnd.equals("")  && classifyDateStart.compareTo(classifyDateEnd)> 0)//如果区间无效，则清空列表
			list =  new ArrayList<CustInfo>();

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", list);
		JSONObject resultJO = JSONObject.fromObject(resultMap);
		out.print(resultJO);
	}

	// 查找十大客户（按贷款余额）
	@RequestMapping("/findBiggestCustomer")
	public void findBiggestCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custType = request.getParameter("custType");
		String custScope = request.getParameter("custScope");
		String exposureType = request.getParameter("exposureType");
		String orgCode = request.getParameter("orgCode");
		String creditLevel = request.getParameter("pdValue");
		String defaultStat = request.getParameter("defaultStat");
		String industryTypeOne = request.getParameter("industryTypeLevelOne");
		String industryTypeTwo = request.getParameter("industryTypeLevelTwo");
		
		String industryType = "";
		if(industryTypeTwo.equals("")){
			if(!industryTypeOne.equals("")) industryType = new StringTokenizer(industryTypeOne).nextToken();
		}
		else industryType = industryTypeTwo;
		
		request.getSession().setAttribute("custType", custType);
		request.getSession().setAttribute("custScope", custScope);
		request.getSession().setAttribute("exposureType", exposureType);
		request.getSession().setAttribute("orgCode", orgCode);
		request.getSession().setAttribute("industryType", industryType);
		request.getSession().setAttribute("creditLevel", creditLevel);
		request.getSession().setAttribute("defaultStat", defaultStat);
		
		request.getSession().setAttribute("fixedReportType", "00");//表明非固定报表
		request.getSession().setAttribute("statisticsQuery", "02");//综合查询-客户查询
		

		List<CustInfo> list = new ArrayList<CustInfo>();
		
		//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
		String exposureTypeString = "";
		if(!exposureType.equals("")){
			exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
		}

		Map<String,String> condition = new LinkedHashMap<String,String>();
		if(custType != null && !custType.equals(""))condition.put("custType", custType);
		if(custScope != null && !custScope.equals(""))condition.put("custScope", custScope);
		if(orgCode != null && !orgCode.equals(""))condition.put("inputOrgId", orgCode);
		if(industryType != null && !industryType.equals(""))condition.put("industryType", industryType);
		if(creditLevel != null && !creditLevel.equals(""))condition.put("creditLevel", creditLevel);
		if(defaultStat != null && !defaultStat.equals(""))condition.put("defaultStat", defaultStat);
		if(exposureTypeString != null && !exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		list = customerService.findCustByCondition(condition,userId,orgId);
		
//		list = customerService.findBiggestCustByCondition(custType,custScope, exposureTypeString, orgCode,industryType, creditLevel,defaultStat,userId,orgId);

		for (CustInfo cust : list) {
			cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId(
					"CustType", cust.getCustType()));
			cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId(
					"CustScope", cust.getCustScope()));
			if (cust.getInputOrgId() != null
					&& !cust.getInputOrgId().equals("")
					&& systemService.findOrgById(cust.getInputOrgId()) != null)
				cust.setInputOrgId(systemService.findOrgById(
						cust.getInputOrgId()).getOrgname());
			cust.setIndustryType(DataDictionary.getInstance()
					.getNameByCodeAndId("IndustryType", cust.getIndustryType()));
			cust.setDefaultStat(DataDictionary.getInstance()
					.getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
//			cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
		}

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", list);
		JSONObject resultJO = JSONObject.fromObject(resultMap);
		out.print(resultJO);
	}

	// 查找债项
	@RequestMapping("/findDebt")
	public void findDebt(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custName = request.getParameter("custName");
		String custType = request.getParameter("custType");
		String custScope = request.getParameter("custScope");
		String orgCode = request.getParameter("orgCode");
		String exposureType = request.getParameter("exposureType");
		String debtId = request.getParameter("debtId");
		String debtName = request.getParameter("debtName");
		String debtType = request.getParameter("debtType");
		String guarantyKind = request.getParameter("guarantyKind");
		String classifyResult = request.getParameter("classifyResult");
		String classifyDate = request.getParameter("classifyDate");
		String industryTypeOne = request.getParameter("industryTypeLevelOne");
		String industryTypeTwo = request.getParameter("industryTypeLevelTwo");
		
		String industryType = "";
		if(industryTypeTwo.equals("")){
			if(!industryTypeOne.equals("")) industryType = new StringTokenizer(industryTypeOne).nextToken();
		}
		else industryType = industryTypeTwo;
		
		if("".equals(classifyDate) || classifyDate == null){//不处理
			classifyDate = "";
		}
		else{//去除月份数字前的“0”
			StringTokenizer dateTonkens = new StringTokenizer(classifyDate, "-");
			String year = dateTonkens.nextToken();
			String month = dateTonkens.nextToken();
			month = "" + Integer.parseInt(month);
			classifyDate = year + "-" + month;
		}

		request.getSession().setAttribute("custName", custName);
		request.getSession().setAttribute("custType", custType);
		request.getSession().setAttribute("custScope", custScope);
		request.getSession().setAttribute("industryType", industryType);
		request.getSession().setAttribute("exposureType", exposureType);
		request.getSession().setAttribute("orgCode", orgCode);
		request.getSession().setAttribute("debtId", debtId);
		request.getSession().setAttribute("debtName", debtName);
		request.getSession().setAttribute("debtType", debtType);
		request.getSession().setAttribute("guarantyKind", guarantyKind);
		request.getSession().setAttribute("classifyResult", classifyResult);
		request.getSession().setAttribute("classifyDate", classifyDate);
		
		request.getSession().setAttribute("fixedReportType", "00");//表明非固定报表
		request.getSession().setAttribute("statisticsQuery", "03");//综合查询-债项查询

		List<DebtInfo> list = new ArrayList<DebtInfo>();

		//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
		String exposureTypeString = "";
		if(!exposureType.equals("")){
			exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
		}
		
		Map<String,String> custCondition = new LinkedHashMap<String,String>();
		Map<String,String> debtCondition = new LinkedHashMap<String,String>();
		//key需要和domain对象的属性相同
		if(!custName.equals(""))custCondition.put("custName", custName);
		if(!custType.equals(""))custCondition.put("custType", custType);
		if(!custScope.equals(""))custCondition.put("custScope", custScope);
		if(!industryType.equals(""))custCondition.put("industryType", industryType);
		if(!exposureTypeString.equals(""))custCondition.put("exposureType", exposureTypeString);
		if(!orgCode.equals(""))custCondition.put("inputOrgId", orgCode);
		
		if(!debtId.equals(""))debtCondition.put("debtId", debtId);
		if(!debtName.equals(""))debtCondition.put("debtDesc", debtName);
		if(!debtType.equals(""))debtCondition.put("debtType", debtType);
		if(!guarantyKind.equals(""))debtCondition.put("guarantyKind", guarantyKind);
		if(!classifyResult.equals(""))debtCondition.put("classifyResult", classifyResult);
		if(!classifyDate.equals(""))debtCondition.put("classifyDate", classifyDate);
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		
		list = customerService.findDebtByCondition(custCondition, debtCondition, userId, orgId);
		
//		list = customerService.findDebtByCondition(custName,custType, custScope, industryType, exposureTypeString, orgCode, debtNo, debtName, debtType, guarantyKind, classifyResult, classifyDate);

		for (DebtInfo debt : list) {
			if(debt.getDebtType()!=null && !debt.getDebtType().equals(""))debt.setDebtType(DataDictionary.getInstance().getNameByCodeAndId("DebtType", debt.getDebtType()));
			if(debt.getGuarantyKind()!=null && !debt.getGuarantyKind().equals(""))debt.setGuarantyKind(DataDictionary.getInstance().getNameByCodeAndId("GuarantyKind", debt.getGuarantyKind()));
			if(debt.getClassifyResult()!=null && !debt.getClassifyResult().equals(""))debt.setClassifyResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", debt.getClassifyResult()));
			
		}

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", list);
		JSONObject resultJO = JSONObject.fromObject(resultMap);
		out.print(resultJO);
	}

}
